package com.imagination.applet.controller.api;

import com.alibaba.fastjson.JSONObject;
import com.imagination.applet.controller.api.request.AddAIImageRequest;
import com.imagination.applet.controller.api.request.cropReq;
import com.imagination.applet.controller.api.response.AIImagesOfChannelResponse;
import com.imagination.applet.controller.api.response.AIImagesOfTopicResponse;
import com.imagination.applet.controller.assembler.AIImagesViewBuilder;
import com.imagination.applet.domain.aiimage.model.AIImage;
import com.imagination.applet.domain.aiimage.service.AIImageCommandService;
import com.imagination.applet.domain.aiimage.service.AIImageQueryService;
import com.imagination.applet.domain.user_topic.service.UserTopicService;
import com.imagination.applet.domain.utils.OssUtil;
import com.imagination.applet.infrastructure.common.RestResponse;
import com.imagination.applet.infrastructure.logging.Log;
//import io.swagger.v3.oas.annotations.Operation;
//import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;



/**
 * ai模板
 */
@RestController
@RequestMapping("/ai-images")
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
//@Tag(name = "AI写真模版服务")
public class AIImagesController {

    private final AIImageQueryService AIImageQueryService;
    private final AIImageCommandService AIImageCommandService;
    private final AIImagesViewBuilder AIImagesViewBuilder;

    private final UserTopicService userTopicService;


    /**
     * channel查询
     * @param channel
     * @return
     */
    @Log
    @GetMapping(value = "/channel-images")
//    @Operation(summary = "频道查询")
    public RestResponse<AIImagesOfChannelResponse> queryChannelImages(@RequestParam(value = "channel") @NotBlank String channel) {
        List<AIImage> images = AIImageQueryService.queryTopGeneratedImagesByChannel(channel);
        return RestResponse.ok(AIImagesViewBuilder.buildChannelVO(images, channel));
    }


    /**
     * 主题查询
     * @param channel
     * @param topic
     * @param pageable
     * @return
     */
    @Log
    @GetMapping(value = "/topic-images")
//    @Operation(summary = "主题查询")
    public RestResponse<AIImagesOfTopicResponse> queryTopicImages(@RequestParam(value = "channel") @NotBlank String channel,
                                                                  @RequestParam(value = "topic") @NotBlank String topic,
                                                                  Pageable pageable) {
        Page<AIImage> images = AIImageQueryService.queryGeneratedImagesByChannelAndTopicPageable(channel, topic, pageable);
        return RestResponse.ok(AIImagesViewBuilder.buildTopicVO(images, channel, topic));
    }


    @Log
    @GetMapping(value = "/buyTopic")
//    @Operation(summary = "购买分类")
    public RestResponse<?> buyTopic(@RequestParam(value = "topicId")@NotNull Long topicId) {
//        Page<AIImage> images = AIImageQueryService.queryGeneratedImagesByChannelAndTopicPageable();
//        return RestResponse.ok(AIImagesViewBuilder.buildTopicVO(images));
        userTopicService.buyTopic(topicId);
        return RestResponse.ok();
    }

    @PostMapping
//    @Operation(summary = "新增写真模版")
    public RestResponse<?> addImage(@RequestPart AddAIImageRequest request, @RequestPart MultipartFile image) {
        AIImageCommandService.createGeneratedImage(request, image);
        return RestResponse.ok();
    }

    @PostMapping("/crop")
//    @Operation(summary = "裁减图片")
    public RestResponse<Object> crop(cropReq cropReq, @RequestPart(name = "file") MultipartFile file) throws Exception {
        JSONObject crop = AIImageCommandService.crop(cropReq, file);
        String croppedFile = crop.getString("cropped_file");
        String replace = OssUtil.getTempUrl(croppedFile).replace("https://voyagerke-test.oss-cn-shenzhen.aliyuncs.com/", "");
        return RestResponse.ok(replace);
    }

}
